package com.zhao.controller;

import com.zhao.Others.DateAndTimeObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller
public class TestController {

    private static final Logger log= LoggerFactory.getLogger(TestController.class);

    @RequestMapping("testt")
    @ResponseBody
    public String print( @RequestParam("image") MultipartFile file){

        System.out.println(file.getOriginalFilename());

        return null;
    }

    List<MyMessage> myMessageList = new ArrayList<>();


    @RequestMapping("toChat.do")
    public String toChat(HttpSession session){
        System.out.println("to send");
        session.setAttribute("myMessageList",myMessageList);
        return "redirect:/aa-mojing";
    }

    @ResponseBody
    @RequestMapping("send.do")
    public String send(HttpSession session,String text){
        System.out.println(text);
        MyMessage myMessage = new MyMessage(text, DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        myMessageList.add(0,myMessage);
        //session.setAttribute("myMessageList",myMessageList);
        System.out.println(session.getId());
        return "success";
    }


    class MyMessage{
        private String dateAndTime;
        private String text;

        public MyMessage() {
        }

        public MyMessage(String dateAndTime, String text) {
            this.dateAndTime = dateAndTime;
            this.text = text;
        }

        public String getDateAndTime() {
            return dateAndTime;
        }

        public void setDateAndTime(String dateAndTime) {
            this.dateAndTime = dateAndTime;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return "myMessage{" +
                    "dateAndTime='" + dateAndTime + '\'' +
                    ", text='" + text + '\'' +
                    '}';
        }
    }
}
